package realtime;

import java.awt.Cursor;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JTextArea;

import application.*;

import messagepasser.*;

public class SendDocumentState extends TimerTask {
	private Document doc;
	private Timer myTimer;
	private String consistentState;
	private boolean noChange = true;

	
	public SendDocumentState(Document d, Timer t) {
		doc = d;
		myTimer = t;
		consistentState = "";
	}
	
	public void run() {
		
		if(doc.getDocId()==1000){
		
			return;
		}
		
		diff_match_patch dmp = new diff_match_patch();
		String currText = doc.contentBox.getText();
		String dest = getOnlineUsers();
		
		
		if (currText.equals(consistentState) == false) {
			LinkedList<diff_match_patch.Diff> diff = dmp.diff_main (consistentState, currText);
			dmp.diff_cleanupSemantic(diff);
			LinkedList<diff_match_patch.Patch> patchList = dmp.patch_make(consistentState, currText, diff);
			
			TimeStampedMessage m = new TimeStampedMessage(dest, doc.getMyPasser().getMyName(), "abc",
					"abc", patchList, false, true);
			m.setMsgType("SendDocumentChanges");
			m.getHeader().setId(doc.getDocId() + "");
			doc.getMyPasser().send(m);
			noChange = false;
		
		}
		else {
			if (noChange != true) {
				TimeStampedMessage m = new TimeStampedMessage(dest, doc.getMyPasser().getMyName(), "abc",
						"abc", currText, false, true);
				m.setMsgType("MulticastDocumentState");
				m.getHeader().setId(doc.getDocId() + "");
				doc.getMyPasser().send(m);
			}
			noChange = true;
		}
		
		Message data = doc.getMyPasser().receive();
		
		
		if (data != null) {
			
			if (data.getMsgType().equals("MulticastDocumentState")) {
				if (noChange) {
					// check the priority
					int position = doc.contentBox.getCaretPosition();
					doc.contentBox.setText((String)data.getPayload());
					try {
						if (position < doc.contentBox.getText().length()) {
							doc.contentBox.setCaretPosition(position);
						}	
					}catch (Exception e) {
						System.out.println ("DBUG : got error in setting position\n");
						
					}
				}
				consistentState = (String)data.getPayload();
				return;
			}
			
			if (doc.getMyPasser().getMyName().contains(data.getPhySender())) {
				return;
			}
			LinkedList<diff_match_patch.Patch> patches = (LinkedList<diff_match_patch.Patch>) (data.getPayload());
			if (patches == null) {
				return;
			}
			Object[] result = dmp.patch_apply(patches, currText);
			consistentState = (String)result[0];
			int position = doc.contentBox.getCaretPosition();
			doc.contentBox.setText((String)result[0]);
			if (position > doc.contentBox.getCaretPosition()) {
				try {
					doc.contentBox.setCaretPosition(position);
				}
				catch (Exception e) {}
			}
			
		}
		else {
			consistentState = currText; 
		}
		
	}

	private String getOnlineUsers() {
		
		String olUsers = "";
		
		for (int i = 0; i < doc.getUsers().size(); i++) {
			if (doc.getUsers().get(i).isOnlinestatus()) {
				olUsers = olUsers + doc.getUsers().get(i).getUsername();
			}
			if (i < doc.getUsers().size() - 1) {
				if (doc.getUsers().get(i).isOnlinestatus()) {
					olUsers = olUsers + ",";
				}
			}
			
		}
		//System.out.println ("online users" + olUsers);
		return olUsers;
	}
}

